﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureCustomizedProfileInstanceUtils.h"
#include "IFamilyConfigElement.h"
#include "IGraphicsCurve3d.h"
#include "IModelLine.h"
#include "IModelView.h"
#include "IPolygon.h"
#include "IPlane.h"
#include "AlgorithmProject.h"
#include "AlgorithmCurveOperate.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

std::wstring gcmp::StructureCustomizedProfileInstanceUtils::GenerateUniqueFamilyName(const IDocument* pDoc, const std::wstring& baseFamilyName)
{
    DBG_WARN_AND_RETURN_UNLESS(pDoc, L"", L"pDoc为空",L"GDMPLab",L"2024-03-30");

    int index = 0;
    // 第一个族的名字：自定义截面梁1
    std::wstring familyName;
    {
        std::wostringstream oss;
        oss << baseFamilyName << ++index;
        familyName = oss.str();
    }
    const IFamilyConfigElement* pFamilyConfigElement = IFamilyConfigElement::GetFamilyConfigElementByName(pDoc, familyName);

    // 后续创建的族序号递增
    while (pFamilyConfigElement)
    {
        std::wostringstream oss;
        oss << baseFamilyName << ++index;
        familyName = oss.str();
        pFamilyConfigElement = IFamilyConfigElement::GetFamilyConfigElementByName(pDoc, familyName);
    }

    return familyName;
}

gcmp::EnLoopsState gcmp::StructureCustomizedProfileInstanceUtils::GeneratePolygonFromModelLines(const std::vector<IModelLine*>& pModelLines,const IModelView* pModelView, std::vector<OwnerPtr<IPolygon>>& opPolygonList)
{
    // IModelLine 转换为 ICurve3d
    std::vector<OwnerPtr<ICurve3d>> profileICurvesOwner;
    std::vector<const ICurve3d*> profileCurves;
    std::vector<OwnerPtr<ICurve3d>> sortedCurves;
    std::vector<int> sortedIndexes;
    FOR_EACH(pModelLine, pModelLines)
    {
        OwnerPtr<ICurve3d> opCurve3d = pModelLine->GetGeometryCurve();
        profileCurves.push_back(opCurve3d.get());
        profileICurvesOwner.push_back(TransferOwnership(opCurve3d));
    }
    gcmp::AlgorithmCurveOperate::CreateSequencedCurves(profileCurves, sortedCurves, sortedIndexes);

    //Coordinate3d coord(Vector3d::Zero, pModelView->GetRightDirection(), pModelView->GetUpDirection());
    OwnerPtr<IPlane> opPlane = IPlane::Create(Vector3d::Zero, pModelView->GetRightDirection(), pModelView->GetUpDirection());
    std::vector<OwnerPtr<ICurve2d>> opCurve2dList;
    FOR_EACH(curve, sortedCurves)
    {
        OwnerPtr<ICurve2d> opCurve2d = AlgorithmProject::Project(curve.get(), opPlane.get());
        opCurve2dList.push_back(TransferOwnership(opCurve2d));
    }

    std::vector<const ICurve2d*> curve2dList;
    FOR_EACH(opCurve2d, opCurve2dList)
    {
        curve2dList.push_back(opCurve2d.get());
    }

    std::map<std::tuple<int, int, int>, int> polygonLoopCoedgeIndexToOriginCrvIndexMap;
    std::vector<const ICurve2d*> errorCurves;
    EnLoopsState loopsState = AlgorithmCurveOperate::Curve2dlistToPolygonLists(curve2dList, opPolygonList, polygonLoopCoedgeIndexToOriginCrvIndexMap, Constants::LENGTH_EPS, errorCurves);
    return loopsState;
}
